<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <!--action设置提交地址
        name 设置提交参数的名称
        limit 设置上传图片的数量
        :on-success="handleSuccess" 设置文件上传成功时调用handleSuccess方法
    -->
    <input type="text" v-model="weibo.content" placeholder="说点啥......">
    <input type="button" value="发布" @click="insert()">
    <el-upload
            action="/upload"
            name="picFile"
            limit="1"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
</div>
</body>
<!--引入axios和vue框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{content:"",url:""},
            }
        },
        methods:{
            handleRemove(file, fileList) {
                console.log(file, fileList);
                //发出一个删除文件的请求,把图片的文件名传递给服务器
                //file.response 得到文件名
                axios.get("/remove?name=" + file.response).then(function () {
                    console.log("删除完成!");
                })
                /*删除图片时把微博对象里面的url清空*/
                v.weibo.url = "";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            handleSuccess(response,file,fileList){
                //response是服务器响应的数据 相当于Axios框架 response.data
                //file代表上传成功的一个文件对象
                //fileList是个数组 里面装着上传成功的所有文件对象
                console.log(response);
                //file文件对象里面有个response属性和单独的response是相等的
                console.log(file.response);
                /*把上传成功后的图片名赋值给weibo对象*/
                v.weibo.url = response;
            },
            insert() {
                if (v.weibo.url == "") {
                    alert("请选择上传图片！");
                    return;
                }
                axios.post("/weibo/insert", v.weibo).then(function (response) {
                    if (response.data == 1) {
                        alert("发布完成");
                        location.href = "/";
                    } else {
                        alert("请先登录！！");
                        location.href = "/login.html";
                    }
                });
            }
        }
    })
</script>
</html>